<?php

namespace app\home\admin;
use app\admin\controller\Admin;
use app\home\model\Charge;
use app\home\model\User;
use think\Db;
use think\Loader;
use think\Request;

class Cashs extends Admin
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function cashlist(Request $request)
    {
        //提现列表
        $data = $request->param();
        $where['c.status'] = ['in', [1,2,3,4]];
        //是否设置了关键字
        if (isset($data['keyword']) && !empty($data['keyword'])) {
            $where['u.nickname'] = ['like', "%{$data['keyword']}%"];
        }
        //是否删选审核状态
        if (isset($data['status']) && $data['status'] != 6) {
            $where['c.status'] = $data['status'];
        }
        if (!empty($data['create_time'])) {
            $time = explode('~', $data['create_time']);
            $start = strtotime($time[0]);
            $end = strtotime($time[1]);
            $where['c.create_time'] = ['between', [$start, $end]];
        }
        isset($data['page']) ? $page = $data['page'] : $page = 1;
        $res = \db('home_cashs')->alias('c')->join('home_users u', 'c.uid = u.id')
            ->field('c.id,c.ordersn,c.rd_coin,c.money,c.create_time,c.check_time,c.status,c.content,u.nickname,u.alipaynum,u.realname')
            ->where($where)
            ->paginate(7)
            ->each(function ($item) {
                $item['create_time'] = date("Y-m-d H:i:s", $item['create_time']);
                $item['check_time'] = empty($item['check_time']) ? '未审核' : date("Y-m-d H:i:s", $item['check_time']);
                $item['content'] = empty($item['content']) ? '无' : $item['content'];
                return $item;
            });
        if ($res) {
            $totalmoney = \db('home_cashs')->where('status',3)->field('sum(money) totalprice')->find();
            $ordermoney = \db('home_cashs')->where('status',2)->field('sum(money) totalprice')->find();
            $alipaymoney = \db('home_cashs')->where(['status'=>4])->field('sum(money) totalprice')->find();
            $wechatmoney = \db('home_cashs')->where(['status'=>1])->field('sum(money) totalprice')->find();
            empty($totalmoney['totalprice']) ? $totalmoney['totalprice'] = '0.00' : true ;
            empty($ordermoney['totalprice']) ? $ordermoney['totalprice'] = '0.00' : true ;
            empty($alipaymoney['totalprice']) ? $alipaymoney['totalprice'] = '0.00' : true ;
            empty($wechatmoney['totalprice']) ? $wechatmoney['totalprice'] = '0.00' : true ;
            $this->assign('totalmoney',$totalmoney['totalprice']);
            $this->assign('ordermoney',$ordermoney['totalprice']);
            $this->assign('alipaymoney',$alipaymoney['totalprice']);
            $this->assign('wechatmoney',$wechatmoney['totalprice']);
            $this->assign('cashlist', $res);
//            $this->assign('role',$data['role']);
        } else {
            return $this->success('无数据');
        }
        return $this->fetch();


    }

    //审核
    public function check(Request $request)
    {
        $data = $request->param();
        if ($request->isPost()){
            $update = [
                'status' => $data['status'],
                'content' => $data['content'],
                'check_time' => time(),
            ];
            $res = \db('home_cashs')
                ->where('id',$data['id'])
                ->update($update);
            if ($data['status'] == 4){
                \db('home_users')->where('id',$data['uid'])->setInc('rd_coin',$data['rd_coin']);
            }
            if ($res){
                return $this->success('操作成功',"/admin.php/home/cashs/cashlist.html");
            }else{
                return $this->error('操作失败,请重试');
            }
        }
        $id = $data['id'];
        $info = \db('home_cashs')->alias('c')->join('home_users u','c.uid = u.id')->where('c.id',$id)
            ->field('c.id,c.ordersn,c.rd_coin,c.money,c.status,c.create_time,c.content,u.nickname,u.alipaynum,u.realname,u.id uid')->find();
        $info['create_time'] = date('Y-m-d H:i:s',$info['create_time']);
        if ($info['status'] == 3){
            return $this->error('该订单已经打款成功,请勿重复操作');
        }
        $this->assign('list',$info);
        return $this->fetch();
    }

}
